<template>
  <div>
    <div class="form-layout">
      <!--左侧导航-->
      <div class="nav-left">
        <customAnchor
          :itemList="[
            { name: 'box-base', title: '基础信息' },
            { name: 'box-access', title: '访问配置' },
            { name: 'box-info', title: '打印配置' },
          ]"
        />
      </div>
      <!--表单区域-->
      <div class="form-right" ref="formRight">
        <customForm :data="form" :rules="propRules" ref="editForm" :disabled="propReadonly">
          <template #content>
            <div id="box-base" class="box-title">基础信息</div>
            <customFormItem
              label="打印机类型"
              name="typeKey"
              :parent="_self"
              :form="form"
              :type="constCommon.formItemType.selectStatic.name"
              :config="{ options: propDictionaries['typeKey'] }"
              @change="selectChange"
            />
            <customFormItem label="名称" name="name" :parent="_self" :form="form" />

            <customFormItem label="位置信息" name="location" :parent="_self" :form="form" />
            <customFormItem
              label="是否校验"
              name="validateFlag"
              :parent="_self"
              :form="form"
              :type="constCommon.formItemType.switch.name"
            />
            <customFormItem label="备注" name="remark" :parent="_self" :form="form" :type="constCommon.formItemType.textarea.name"/>

            <div id="box-access" class="box-title">访问配置</div>
            <customFormItem label="地址" name="url" :parent="_self" :form="form" />
            <customFormItem label="端口" name="port" :parent="_self" :form="form" :type="constCommon.formItemType.inputNumber.name"/>
            <customFormItem label="读写超时时间" name="waittime" :parent="_self" :form="form" help="时间单位为毫秒" :type="constCommon.formItemType.inputNumber.name"/>

            <div id="box-info" class="box-title">打印配置</div>
            <customFormItem label="起始符号" name="startSymbol" :parent="_self" :form="form" />
            <customFormItem label="有效长度" name="effectiveLength" :parent="_self" :form="form" :type="constCommon.formItemType.inputNumber.name"/>
            <customFormItem
              label="单页样式"
              name="stylePage"
              :parent="_self"
              :form="form"
              help="普通标签的样式，json格式，如: {padding:'0px 0px 0px 40px'}"
              :type="constCommon.formItemType.textarea.name"
              fullWidth
            />
            <customFormItem ref="labelCode" label="指令模板" name="labelCode" :parent="_self" :form="form"  :type="constCommon.formItemType.textarea.name" fullWidth/>

          </template>
        </customForm>
      </div>
    </div>
  </div>
</template>

<script>
/*eslint-disabled*/
import customAnchor from '@/components/custom/form/customAnchor.vue'

export default {
  data() {
    return {
      form: {},
      viewData: {},
    }
  },
  methods: {
    selectChange(item, selectItem) {
      this.$set(this.form, 'typeName', this.propDictionaries.typeKey.filter((ob) => ob.value == item)[0].label)
    },
  },
  created() {
    this.$emit('event-register', {
      validate: (callback) => {
        return this.$refs.editForm.validate(callback)
      },
      beforeSave: () => {
        this.$emit('data-callback', this.form)
        if (this.form.typeKey == 'rfid' && this.form.labelCode.length < 6) {
          this.utilMessage.warning('RFID打印机必须指定指令模板')
          this.$refs.labelCode.focus()
          return false
        }
        if (this.form.typeKey == 'rfidOut' && this.form.labelCode.length < 6) {
          this.utilMessage.warning('RFID打印机（外部调用）必须指定指令模板')
          this.$refs.labelCode.focus()
          return false
        }
        return true
      },
      afterMove: (formData, initData) => {
        if (this.form.typeKey == 'rfidOut' && this.form.validateFlag == 1 && this.form.effectiveLength < 1) {
          this.utilMessage.warning('RFID打印机（外部调用）必须指定RFID的有效长度')
          this.$refs.effectiveLength.focus()
          return false
        }
        this.form = global.utils.jsonClone(formData)
        // 设置switch 元素默认值
      },
      afterSave: () => {},
      beforeDelete: () => {
        return true
      },
      afterDelete: () => {},
      beforeFlow: (flowButtonKey) => {
        return true
      },
      afterClick: (param) => {
        if (param.key == 'test') {
          //打印机连接测试
          if (!this.form.url) {
            this.utilMessage.warning('请先设置访问地址')
            this.$refs.url.focus()
            return
          }
          if (!this.form.port) {
            this.utilMessage.warning('请先设置端口')
            this.$refs.port.focus()
            return
          }
          this.utilVue.loadShow(true)
          this.$ajax.get(`/tool/printTest?url=${this.form.url}&port=${this.form.port}`).then((resp) => {
            if (resp.code == 0) {
              this.utilMessage.success(resp.msg)
            }
            this.utilVue.loadHide()
          })
        }
      },
      beforeClose: () => {
        return true
      },
    })
  },
  props: {
    propParent: Object,
    propInitData: Object,
    propPrimaryKey: String,
    propDictionaries: Object,
    propCurrentRow: Object,
    propViewFormData: Object,
    propReadonly: Boolean,
    propStatus: String,
    propRules: Object,
  },
  components: { customAnchor },
  computed: {},
}
</script>
